package com.cnkvha.trackcontestmgmt.ui.track;

import com.cnkvha.trackcontestmgmt.contest.Contest;
import com.cnkvha.trackcontestmgmt.contest.ContestTrackInfo;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

public class TrackTableCellRenderer extends DefaultTableCellRenderer {

    public TrackTableCellRenderer() {
        setHorizontalAlignment(CENTER);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Contest contest = (Contest) value;
        ContestTrackInfo t = contest.tracks[row];
        String text;
        if(column == 0) {
            text = "";
        } else if (column == 1) {
            text = t.name;
        } else if (column == 2) {
            text = t.participant;
        } else if (column == 3) {
            switch (contest.state) {
                case STANDBY:
                    text = "等待中";
                    break;
                case WAITING:
                    text = "等待发令";
                    break;
                case TRACKING:
                    if(t.received_timer_stop == 0) {
                        text = (float) (System.currentTimeMillis() - contest.start_time) / 1000 + " ...";
                    } else {
                        text = "~" + (float) (t.avg_time_spent) / 1000 + " ...";
                    }
                    break;
                case FINALIZING:
                    text = "~" + (float) (t.avg_time_spent) / 1000;
                    break;
                case FINISHED:
                    text = (float) t.confirmed_time_spent / 1000 + " √";
                    break;
                default:
                    text = "?";
                    break;
            }
        } else if (column == 4) {
            text = Integer.toString(t.count_timer);
        } else if (column == 5) {
            if(t.realtime_order == -1) {
                text = "?";
            } else {
                if(t.confirmed) {
                    text = "#" + t.confirmed_order;
                } else {
                    text = "#" + t.realtime_order;
                }
            }
        } else {
            text = "?";
        }
        Component c = super.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, column);
        if(column == 0) {
            if (System.currentTimeMillis() / 500 % 2 == 0) {
                c.setBackground(Color.BLACK);
            } else {
                switch (contest.state) {
                    case STANDBY:
                        c.setBackground(Color.RED);
                        break;
                    case WAITING:
                        c.setBackground(Color.YELLOW);
                        break;
                    case TRACKING:
                        if(t.confirmed) {
                            c.setBackground(Color.CYAN);
                        } else if (t.received_timer_stop == 0) {
                            c.setBackground(Color.GREEN);
                        } else {
                            c.setBackground(new Color(0x0041F3));
                        }
                        break;
                    case FINALIZING:
                    case FINISHED:
                        c.setBackground(new Color(0, 126, 42));
                        break;
                    default:
                        c.setBackground(Color.MAGENTA);
                        break;
                }
            }
        }
        return c;
    }
}
